﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ChatServerApplication
{
    class Server
    {
        public Server(int Port, int Backlog)
        {
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(IPAddress.Any, Port));
            server.Listen(Backlog);

            logText("Server now listening for Clients...");

            while (true)
            {
                Socket sock = server.Accept();
                ServerClientHandling handling = new ServerClientHandling(sock);
                Thread clientthread = new Thread(new ThreadStart(handling.Run));
                clientthread.Start();
            }
        }

        private void logText(String text)
        {
            ChatServerApplication.dle.Invoke(text + Tools.NL);
        }
    
    }
}
